#include "game.h"
#include "ui_game.h"
#include "qobject.h"


Game::Game(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Game)
{
    ui->setupUi(this);
    for(int i=0;i<8;i++)
    {
        cd[i] = new Card(this);
        connect(cd[i],&Card::sendSelf,this,&Game::getObject);
        cd[i]->move(QRandomGenerator::global()->bounded(500.00),QRandomGenerator::global()->bounded(500.00));
    }

}

Game::~Game()
{
    delete ui;
}


//获取控件坐标
void Game::getObject(Card *w)
{
    temp = w;
    startP = cursor().pos()-this->pos();
    yuanP = temp->pos();
    //将此小部件提升到父小部件堆栈的顶部
    temp->raise();
}

//移动事件
void Game::mouseMoveEvent(QMouseEvent *event)
{
    temp->move(yuanP.x()+event->x()-startP.x(),yuanP.y()+event->y()-startP.y());
}

//拖拽对象置顶，卡牌积压的时候，拖动的那张卡牌置顶
void Game::mouseReleaseEvent(QMouseEvent *event)
{
    temp->raise();
}

void Game::paintEvent(QPaintEvent *ev)
{
    static int pos=0;
    QString pictures[2]={":/res/tabletop.png",""};
    QPainter *painter =new QPainter(this);


    QPixmap pixmap;
    pixmap.load(pictures[pos]);
    pixmap.scaled(512,512);
    painter->drawPixmap(0,0,512,512,pixmap);
}



